const PROGRESS_STEP = 0.01


cc.Class({
    extends: cc.Component,

    properties: {
        progress: cc.ProgressBar,
        complete: null

    },

    // use this for initialization
    onLoad: function () {
        this.progress.node.active = false
    },

    startCount(time) {
        this.stopCount()
        this.progress.node.active = true
        this.schedule(this.count, PROGRESS_STEP * time, 1 / PROGRESS_STEP)
    },

    stopCount() {
        this.progress.node.active = false
        this.progress.progress = 0
        this.unschedule(this.count)
    },

    count() {
        this.progress.progress += PROGRESS_STEP
        if (1 <= this.progress.progress) {
            this.stopCount()
            if (this.complete) {
                this.complete()
            }
        }
    },

    startFillingCount(time) {
        this.stopFillingCount()
        this.progress.node.active = true
        this.progress.progress = 1
        this.schedule(this.fillingCount, PROGRESS_STEP * time, 1 / PROGRESS_STEP)
    },

    stopFillingCount() {
        this.progress.progress = 0
        this.progress.node.active = false
        this.unschedule(this.fillingCount)
    },

    fillingCount() {
        this.progress.progress -= PROGRESS_STEP
        if (this.progress.progress <= 0) {
            this.stopFillingCount()
            if (this.complete) {
                this.complete()
            }
        }
    }
});
